package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.constant.WechatConstant;
import com.controller.common.CommonController;
import com.scanner.Invoker;
import com.scanner.InvokerHolder;
import com.util.WechatSignatureUtil;

@Controller
@RequestMapping()
public class TestController extends CommonController{

	@ResponseBody
	@RequestMapping("/test")
	public Object test(String module, String cmd){
		
		System.err.println("module: " + module);
		System.err.println("cmd: " + cmd);
		
		Invoker invoker = InvokerHolder.getInvoker(module, cmd);
		invoker.invoke(null);
		
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("test", "value");
		
		return resultMap;
	}
	
	@RequestMapping("/wechat_api")
	public void wechatApi(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response){

		System.err.println("signature: " + signature);
		System.err.println("timestamp: " + timestamp);
		System.err.println("nonce "+nonce);
		System.err.println("echostr "+echostr);
		
        //1. 将token、timestamp、nonce三个参数进行字典序排序  
		String[] arrTmp = { WechatConstant.Wechat_Server_Token, timestamp, nonce };
        
		if(!WechatSignatureUtil.checkWechatSignature(WechatConstant.Wechat_Server_Token, timestamp, nonce, signature)){
			//false: 验证失败
			this.printSimpleResult(response, "check signature false.");
			System.out.println("check signature fail.");
		}else{
			//true: 验证成功
			this.printSimpleResult(response, echostr);
			System.out.println("check signature success.");
		}
		
	}
	
}
